package com.jflm.base;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.jflm.common.Contants;
import com.jflm.service.IPropertyService;
import com.jflm.util.PropertiesUtils;

/**
 * ******************  类说明  *********************
 * class       :  BaseAction
 * @author     :  hejinyun@umpay.com
 * @version    :  1.0  
 * description :  mall-gate接口服务基类
 * @see        :                        
 * ***********************************************
 */
@Controller
public class BaseAction {
	
	private static final Logger LOG = LoggerFactory.getLogger(BaseAction.class);
	@Autowired
	private IPropertyService propertyService;
	@Autowired
	protected Gson gson;
	
	/**
	 * ********************************************
	 * method name   : getServiceDomain 
	 * description   : 获取服务域名
	 * @return       : String
	 * @param        : @param appid
	 * @param        : @return
	 * modified      : hejinyun@umpay.com ,  2017-4-28  下午10:49:26
	 * @see          : 
	 * *******************************************
	 */
	public String getServiceDomain(String appid){
				
		String domain = propertyService.getStrValue(appid+".domain", "");
		return domain;
	}
	/**
	 * ********************************************
	 * method name   : getRedPacketServiceDomain 
	 * description   : 
	 * @return       : String
	 * @param        : @return
	 * modified      : shilele ,  2017年6月12日  下午6:28:35
	 * @see          : 
	 * *******************************************
	 */
	public String getRedPacketServiceDomain(){
		String domain = PropertiesUtils.getProperty("mall_service.domain.url");
		return domain;
	}
	/**
	 * ********************************************
	 * method name   : getServiceURL 
	 * description   : 获取服务请求url
	 * @return       : String
	 * @param        : @param appid
	 * @param        : @param uri
	 * @param        : @return
	 * modified      : hejinyun@umpay.com ,  2017-4-28  下午10:50:19
	 * @see          : 
	 * *******************************************
	 */
	public String getServiceURL(String appid, String uri){
		String domain = this.getServiceDomain(appid);
		return getServiceURL(domain, uri);
	}
	
	
	/**
	 * ********************************************
	 * method name   : getServiceURL 
	 * description   : 获取服务请求url
	 * @return       : String
	 * @param        : @param appid
	 * @param        : @param domain
	 * @param        : @param uri
	 * @param        : @return
	 * modified      : hejinyun@umpay.com ,  2017-4-28  下午10:50:01
	 * @see          : 
	 * *******************************************
	 */
	public String getServiceURL(String appid, String domain, String uri){
		StringBuffer buffer = new StringBuffer();
		buffer.append(domain);
		buffer.append("/");
		buffer.append(uri);
		return buffer.toString();
	}
	
	/**
	 * ********************************************
	 * method name   : read 
	 * description   : 读取
	 * @return       : String
	 * @param        : @param request
	 * @param        : @param decoder
	 * @param        : @return
	 * @param        : @throws Exception
	 * modified      : hejinyun@umpay.com ,  2017-4-28  下午9:38:32
	 * @see          : 
	 * *******************************************
	 */
	public String read(HttpServletRequest request, boolean decoder)
			throws Exception {
		String method = request.getMethod();
		String contentType = request.getContentType();
		LOG.info("#read() Content-Type:{}", contentType);
		String encoding = request.getCharacterEncoding();
		encoding = (null == encoding) ? Contants.CHARSET_GBK : encoding;
		if (Contants.HTTP_METHOD_POST.equalsIgnoreCase(method)) {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					request.getInputStream(), encoding));
			StringBuffer buffer = new StringBuffer();
			String line = null;
			while (null != (line = br.readLine())) {
				buffer.append(line);
			}
			return decoder ? URLDecoder.decode(buffer.toString(), Contants.CHARSET_UTF_8):buffer.toString();
		}
		return decoder ? URLDecoder.decode(request.getQueryString(), Contants.CHARSET_UTF_8):request.getQueryString();
	}
	
	/**
	 * ********************************************
	 * method name   : write 
	 * description   : 写入
	 * @return       : void
	 * @param        : @param rpid
	 * @param        : @param response
	 * @param        : @param content
	 * @param        : @param contentType
	 * @param        : @throws Exception
	 * modified      : hejinyun@umpay.com ,  2017-4-28  下午9:41:37
	 * @see          : 
	 * *******************************************
	 */
	public void write(String rpid, HttpServletResponse response, 
			String content, String contentType) throws Exception{
		LOG.info("#write() Rpid:{},响应报文:{}", rpid, content);
		response.setContentType(contentType);
		PrintWriter writer = response.getWriter();
		writer.write(content);
		writer.flush();
		writer.close();
	}
	
	/**
	 * ********************************************
	 * method name   : write 
	 * description   : 写入
	 * @return       : void
	 * @param        : @param response
	 * @param        : @param obj
	 * @param        : @throws Exception
	 * modified      : zhangyajie1@umpay.com ,  2017-4-30  下午11:50:27
	 * @see          : 
	 * *******************************************
	 */
	public void write(HttpServletResponse response, Object obj) throws Exception{
		String content = "";
		if(obj != null){
			content = gson.toJson(obj);
		}
		this.write(response, content, "application/xml; charset=utf-8");
	}
	
	/**
	 * ********************************************
	 * method name   : write 
	 * description   : 写入
	 * @return       : void
	 * @param        : @param response
	 * @param        : @param content
	 * @param        : @param contentType
	 * @param        : @throws Exception
	 * modified      : hejinyun@umpay.com ,  2017-4-30  下午11:50:27
	 * @see          : 
	 * *******************************************
	 */
	public void write(HttpServletResponse response, 
			String content, String contentType) throws Exception{
		response.setContentType(contentType);
		PrintWriter writer = response.getWriter();
		writer.write(content);
		writer.flush();
		writer.close();
	}
	
	/**
	 * ********************************************
	 * method name   : write 
	 * description   : 写入
	 * @return       : void
	 * @param        : @param response
	 * @param        : @param content
	 * @param        : @throws Exception
	 * modified      : hejinyun@umpay.com ,  2017-4-30  下午11:51:51
	 * @see          : 
	 * *******************************************
	 */
	public void write(HttpServletResponse response, 
			String content) throws Exception{
		this.write(response, content, "application/xml; charset=utf-8");
	}
	
	/**
	 * ********************************************
	 * method name   : getClientIpAddr 
	 * description   : 获取远程客户端请求ip地址
	 * @return       : String
	 * @param        : @param request
	 * @param        : @return
	 * @param        : @throws Exception
	 * modified      : hejinyun@umpay.com ,  2017-4-28  下午9:30:38
	 * @see          : 
	 * *******************************************
	 */
	public static String getClientIpAddr(HttpServletRequest request) throws Exception {
		String ip = request.getHeader("X-Real-IP");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
	
}
